package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.CommonProblem;
import com.antdesignbackend.backend.service.CommonProblemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/commonProblem")
public class CommonProblemController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    @Autowired
    CommonProblemService commonProblemService;
    /**
     *常见问题
     */
    @GetMapping("/index")
    @AuthToken
    public String index() {
        return "commonProblem/index";
    }
    /**
     * 表格查询
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody CommonProblem commonProblem) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<CommonProblem> commonProblems=new ArrayList<>();
        QueryWrapper<CommonProblem> wrapper =new QueryWrapper<>();
        if(commonProblem.getProblem()!=null&& !commonProblem.getProblem().equals(""))
        {
            wrapper.like("problem",commonProblem.getProblem());
        }
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=commonProblemService.count(wrapper);
        result.setTotal(total);
        System.out.println("*******************************满足条件的数据有："+total);
        //再查询
        if(total>=0)
        {
            commonProblems=commonProblemService.list(wrapper);
        }
        result.setData(commonProblems);
        return result;
    }
    /**
     * 根据id删除
     */
    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public ReturnVO deleteById(@PathVariable Serializable id) {
        result = new ReturnVO();
        if (commonProblemService.removeById(Integer.parseInt(id.toString())) == true) {
            result.setMessage("删除成功");
        } else {
            result.setMessage("删除失败");
            result.setCode("0");
        }
        return result;
    }
    /**
     * 修改/新增操作的保存
     */
    @PostMapping("/save")
    @ResponseBody
    @AuthToken
    public ReturnVO save(@RequestBody CommonProblem commonProblem, HttpServletRequest request) throws Exception {
        result = new ReturnVO();
        boolean b = false;
        if(commonProblem.getProblemId()==0){
            b=commonProblemService.save(commonProblem);
        }else{
            b=commonProblemService.updateById(commonProblem);
        }
        if(b==true){
            result.setMessage("保存成功！");
            result.setCode("200");
        }
        else
        {
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }
}
